package main

import "sync"

var (
	//一个演示用的映射
	valueByKey = make(map[string]int)
	valueByKeyGuard sync.Mutex
)

//根据键读取值
func readValue(key string) int {
	//对共享资源加锁
	valueByKeyGuard.Lock()
	// 取值
	v := valueByKey[key]

	valueByKeyGuard.Unlock()
	return v;
}

func readValue2(key string) int {
	valueByKeyGuard.Lock()
	defer valueByKeyGuard.Unlock()
	return valueByKey[key]
}